package com.backend.controllers;

import com.backend.qiniu_manager.ConfigProperties;
import com.backend.qiniu_manager.QiNiuConfig;
import com.qiniu.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UploadInterfaceController {
    @Autowired
    ConfigProperties configProperties;

    @RequestMapping(value = "/get_upload_token/", method = RequestMethod.GET)
    @ResponseBody
    @Cacheable(value = "myCache", key = "'upload_token'")
    public String getUploadToken(HttpServletRequest request) {
        String key = request.getParameter("key");
        //确保如果key为空串的时候key 能为null 。key is not null or empty,生成覆盖上传凭证
        if (StringUtils.isNullOrEmpty(key)) {
            key = null;
        }
        boolean override = key == null ? false : true;
        QiNiuConfig qiNiuConfig = QiNiuConfig.getInstance(configProperties);
        String uploadToken = qiNiuConfig.getUploadToken(key, override);
        return uploadToken;
    }
}
